/**
 * 
 */
package com.gmail.blackdog1987.carcare.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gmail.blackdog1987.carcare.dao.CleanShopDao;
import com.gmail.blackdog1987.carcare.dao.DriveSchoolDao;
import com.gmail.blackdog1987.carcare.dao.OrderDriveCompanyDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.carcare.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.carcare.entity.TCarchangeshop;
import com.gmail.blackdog1987.carcare.entity.TCarchangeshopDetail;
import com.gmail.blackdog1987.carcare.entity.TCarfixshop;
import com.gmail.blackdog1987.carcare.entity.TCleanshop;
import com.gmail.blackdog1987.carcare.entity.TDriveschool;
import com.gmail.blackdog1987.carcare.entity.TMaintainDetail;
import com.gmail.blackdog1987.carcare.entity.base.Page;
import com.gmail.blackdog1987.carcare.service.ShopService;
import com.gmail.blackdog1987.carcare.util.ErrorCode;

/**
 * 
 * @author 董国超
 *
 */
@Service("shopServiceImpl")
public class ShopServiceImpl implements ShopService {

	@Autowired
	private BaseJdbcDao baseJdbcDao;
	@Autowired
	private CleanShopDao cleanShopDao;

	@Override
	public Map<String, Object> getDriveSchool(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("code", ErrorCode.NO);

		String lat = filter.get("lat");
		String lon = filter.get("lon");
		String order = filter.get("order");

		String pageIndex = filter.get("pageindex");
		String pageSize = filter.get("pagesize");

		double latDouble = lat == null || lat.length() == 0 ? 0 : Double
				.valueOf(lat);
		double lonDouble = lon == null || lon.length() == 0 ? 0 : Double
				.valueOf(lon);

		int index = 1;
		int size = 999;
		if (pageIndex != null && pageIndex.length() != 0) {
			index = Integer.valueOf(pageIndex);
		}

		if (index < 1) {
			index = 1;
		}
		if (pageSize != null && pageSize.length() != 0) {
			size = Integer.valueOf(pageSize);
		}

		if (lat == null || lat.length() == 0) {
			lat = "0";
		}
		if (lon == null || lon.length() == 0) {
			lon = "0";
		}

		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		if ("2".equals(order)) {
			sql = " from t_driveschool order by price ";
		} else {
			sql = " from t_driveschool order by getDistance(?, ?, lat, lon) ";
			whereArgs.add(latDouble);
			whereArgs.add(lonDouble);
		}

		String limit = " limit " + (index - 1) * size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(
				" select count(*) " + sql, whereArgs.toArray());

		List<TDriveschool> schools = JdbcUtil.queryForList(baseJdbcDao,
				TDriveschool.class, " select * " + sql + limit,
				whereArgs.toArray());

		Page page = new Page(schools, index, totalCount, size);

		result.put("result", page);

		return result;
	}

	@Override
	public Map<String, Object> getCleanShop(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("code", ErrorCode.NO);

		String lat = filter.get("lat");
		String lon = filter.get("lon");
		String order = filter.get("order");

		String pageIndex = filter.get("pageindex");
		String pageSize = filter.get("pagesize");

		double latDouble = lat == null || lat.length() == 0 ? 0 : Double
				.valueOf(lat);
		double lonDouble = lon == null || lon.length() == 0 ? 0 : Double
				.valueOf(lon);

		int index = 1;
		int size = 999;
		if (pageIndex != null && pageIndex.length() != 0) {
			index = Integer.valueOf(pageIndex);
		}

		if (index < 1) {
			index = 1;
		}
		if (pageSize != null && pageSize.length() != 0) {
			size = Integer.valueOf(pageSize);
		}

		if (lat == null || lat.length() == 0) {
			lat = "0";
		}
		if (lon == null || lon.length() == 0) {
			lon = "0";
		}

		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		if ("2".equals(order)) {
			sql = " from t_cleanshop order by price ";
		} else {
			sql = " from t_cleanshop order by getDistance(?, ?, lat, lon) ";
			whereArgs.add(latDouble);
			whereArgs.add(lonDouble);
		}

		String limit = " limit " + (index - 1) * size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(
				" select count(*) " + sql, whereArgs.toArray());

		List<TCleanshop> shops = JdbcUtil.queryForList(baseJdbcDao,
				TCleanshop.class, " select * " + sql + limit,
				whereArgs.toArray());

		Page page = new Page(shops, index, totalCount, size);

		result.put("result", page);

		return result;
	}

	@Override
	public Map<String, Object> getFixShop(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("code", ErrorCode.NO);

		String lat = filter.get("lat");
		String lon = filter.get("lon");
		String order = filter.get("order");

		String pageIndex = filter.get("pageindex");
		String pageSize = filter.get("pagesize");

		double latDouble = lat == null || lat.length() == 0 ? 0 : Double
				.valueOf(lat);
		double lonDouble = lon == null || lon.length() == 0 ? 0 : Double
				.valueOf(lon);

		int index = 1;
		int size = 999;
		if (pageIndex != null && pageIndex.length() != 0) {
			index = Integer.valueOf(pageIndex);
		}

		if (index < 1) {
			index = 1;
		}
		if (pageSize != null && pageSize.length() != 0) {
			size = Integer.valueOf(pageSize);
		}

		if (lat == null || lat.length() == 0) {
			lat = "0";
		}
		if (lon == null || lon.length() == 0) {
			lon = "0";
		}

		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		sql = " from t_carfixshop where 1=1 ";
		String sqlOrder = "";
		if ("1".equals(order)) {
			sqlOrder = " order by getDistance(?, ?, lat, lon) ";
			whereArgs.add(latDouble);
			whereArgs.add(lonDouble);
		}

		if ("2".equals(order)) {
			sqlOrder = " order by price ";
		}

		String limit = " limit " + (index - 1) * size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(
				" select count(*) " + sql, new Object[] {});

		List<TCarfixshop> shops = JdbcUtil.queryForList(baseJdbcDao,
				TCarfixshop.class, " select * " + sql + sqlOrder + limit,
				whereArgs.toArray());

		Page page = new Page(shops, index, totalCount, size);

		result.put("result", page);

		return result;
	}

	@Override
	public Map<String, Object> getChangeShop(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("code", ErrorCode.NO);

		String lat = filter.get("lat");
		String lon = filter.get("lon");
		String order = filter.get("order");
		String type = filter.get("type");

		String pageIndex = filter.get("pageindex");
		String pageSize = filter.get("pagesize");

		double latDouble = lat == null || lat.length() == 0 ? 0 : Double
				.valueOf(lat);
		double lonDouble = lon == null || lon.length() == 0 ? 0 : Double
				.valueOf(lon);

		int index = 1;
		int size = 999;
		if (pageIndex != null && pageIndex.length() != 0) {
			index = Integer.valueOf(pageIndex);
		}

		if (index < 1) {
			index = 1;
		}
		if (pageSize != null && pageSize.length() != 0) {
			size = Integer.valueOf(pageSize);
		}

		if (lat == null || lat.length() == 0) {
			lat = "0";
		}
		if (lon == null || lon.length() == 0) {
			lon = "0";
		}

		int typeInt = 1;
		if (type != null && type.length() != 0) {
			typeInt = Integer.valueOf(type);
		}

		String sql;
		ArrayList<Object> whereArgs = new ArrayList<Object>();
		sql = " from t_carchangeshop where type=? ";
		String sqlOrder = "";
		whereArgs.add(typeInt);
		if ("1".equals(order)) {
			sqlOrder = " order by getDistance(?, ?, lat, lon) ";
			whereArgs.add(latDouble);
			whereArgs.add(lonDouble);
		}

		if ("2".equals(order)) {
			sqlOrder = " order by price ";
		}

		String limit = " limit " + (index - 1) * size + "," + size;
		Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(
				" select count(*) " + sql, new Object[] { whereArgs.get(0) });

		List<TCarchangeshop> shops = JdbcUtil.queryForList(baseJdbcDao,
				TCarchangeshop.class, " select * " + sql + sqlOrder + limit,
				whereArgs.toArray());

		Page page = new Page(shops, index, totalCount, size);

		result.put("result", page);

		return result;
	}

	@Override
	public Map<String, Object> getChangeDetail(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("code", ErrorCode.NO);

		String shopid = filter.get("shopid");
		String sql = "select * from t_carchangeshop_detail where shopid=? ";

		List<TCarchangeshopDetail> details = JdbcUtil.queryForList(baseJdbcDao,
				TCarchangeshopDetail.class, sql, new Object[] { shopid });
		result.put("result", details);
		return result;
	}

	@Override
	public Map<String, Object> getCleanShopDetail(Map<String, String> filter) {
		Map<String, Object> result = new HashMap<String, Object>();

		result.put("code", ErrorCode.NO);

		String shopid = filter.get("shopid");
		TCleanshop shop = cleanShopDao.get(Integer.valueOf(shopid));
		result.put("result", shop);
		return result;
	}
}
